package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.AssetsImagesMapper;
import com.zygh.policeasset.pojo.AssetsImages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/AssetsImagesService.class */
public class AssetsImagesService {
    @Autowired
    AssetsImagesMapper assetsImagesMapper;
    @Autowired
    MinioService minioService;
    private String JpgContentType = "image/jpeg";

    public Integer add(AssetsImages assetsImages) {
        int count = 0;
        try {
            String objectName = assetsImages.getType() + "/" + assetsImages.getBuildNum() + "/" + IdUtil.objectId() + ".jpg";
            this.minioService.upload(objectName, assetsImages.getIcon().getInputStream(), this.JpgContentType, "assets");
            count = this.assetsImagesMapper.add(assetsImages.getAssetsId(), "http://41.6.251.17:9000/assets/" + objectName, DateUtil.now());
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return count;
        }
    }

    public CommonResult getImageUrl(AssetsImages assetsImages) {
        try {
            String objectName = assetsImages.getType() + "/" + assetsImages.getBuildNum() + "/" + IdUtil.objectId() + ".jpg";
            minioService.upload(objectName, assetsImages.getIcon().getInputStream(), this.JpgContentType, "assets");
            return new CommonResult().success("http://41.6.251.17:9000/assets/" + objectName);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed("添加失败");
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer delete(Integer id) {
        int count = 0;
        try {
            this.minioService.removeObject("assets", this.assetsImagesMapper.findById(id).getConnect().split("http://41.6.251.17:9000/assets/")[1]);
            count = this.assetsImagesMapper.delete(id);
            return count;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return count;
        }
    }

    public AssetsImages findByAssetsId(Integer assetsId) {
        return this.assetsImagesMapper.findByAssetsId(assetsId);
    }
}